/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#pragma once

#include "Message.h"
#include "PostOffice.h"

namespace XF
{
	class Messenger
	{
	private:
		static PostOffice *msPostOffice;
		static int msNextUID;
		std::string mPhonebookName;

		int mUID;

	protected:
		virtual bool OnHandleMessage(const Message &message) { return false; }

	public:
		Messenger();
		virtual ~Messenger();

		bool HandleMessage(const Message &message){ return OnHandleMessage(message); }
		
		bool MessageSelf(MessageSubject::ENUM subject, void* data = NULL)//Chris Added this function for streamlining component internal message handling
		{
			Message message;
			message.ReceiverID = mUID;
			message.SenderID = mUID;
			message.SendTime = 0;
			message.Subject = subject;
			message.Data = data;
			return OnHandleMessage(message);
		}

	
		void PostAMessage(int receiverID, MessageSubject::ENUM subject, void* data = NULL, float sendDelay = 0); //chris i just added default parameters to clean things up a little and make them easier
	
		void PostAMessage(XF::MailingList::ENUM mailingList, MessageSubject::ENUM subject, void* data = NULL, float sendDelay = 0);
		

		bool SubscribeToMailingList(XF::MailingList::ENUM mList);
		void UnsubscribeFromMailingList(XF::MailingList::ENUM mList);

		void RegisterInPhonebook(std::string name);
		void UnregisterFromPhonebook();
		int LookupPhonebook(std::string name);
		bool LookupPhonebookSafe( std::string name,int& id ); //chris

		void ThrowError(std::string error) //chris i added this so all messengers can easily send of a error message
		{
			PostAMessage(XF::MailingList::ERROR_HANDLERS, XF::MessageSubject::ERROR_MESSAGE, &std::string(error));
		}

		int ID(){return mUID;}

		static void PostOffice(PostOffice * val) 
		{ 
			msPostOffice = val; 
		}
		
	};
}
